const chineseNumbers = [
  '一',
  '二',
  '三',
  '四',
  '五',
  '六',
  '七',
  '八',
  '九',
  '十'
]
const chineseUnitNames = ['', '十', '百', '千'] // 用于表示十位、百位、千位

// 转换数字转目录索引
export function convertToDocxIndex(type: number, num: number) {
  if (!num) return ''

  switch (type) {
    // 一、
    case 1:
      return `${convertToChineseNumber(num)}、`
    //（一）
    case 2:
      return `（${convertToChineseNumber(num)}）`
    // 1.
    case 3:
      return `${num}.`
  }
}

function convertToChineseNumber(num) {
  if (num > 0 && num <= 9999) {
    const numStr = num.toString()
    let result = ''

    for (let i = 0; i < numStr.length; i++) {
      const digit = parseInt(numStr[i], 10)

      if (digit !== 0) {
        // Add a special case for handling 十
        if (numStr.length > 1 && i === 0 && digit === 1) {
          result += chineseUnitNames[numStr.length - i - 1]
        } else {
          result +=
            chineseNumbers[digit - 1] + chineseUnitNames[numStr.length - i - 1]
        }
      }
    }

    return result
  } else {
    return num.toString()
  }
}
